﻿using Microsoft.EntityFrameworkCore;
using SheXiangTou.Beans.LocalModel;
using SheXiangTou.Tools;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace SheXiangTou.Services
{
    public class LocalDatabaseService
    {
        public static void AddAlarm_Async(TbRzAlarm tbRzAlarm)
        {
            Task.Run(() =>
            {
                using (renzhaContext r = new renzhaContext())
                {
                    try
                    {
                        TAlarm alarm = new TAlarm
                        {
                            AlarmType = tbRzAlarm.AlarmType.ToString(),
                            ProjectId = 1,
                            Phone = tbRzAlarm.CardNum,
                            AlarmInfo = tbRzAlarm.AlarmMessage,
                            ImgUrl = tbRzAlarm.HeadPicture,
                            Action = "进",
                            Type = 1,
                            CreateTime = DateTime.Now,
                            Status = 0
                        };
                        tbRzAlarm.Id = MathTool.nextId(3);
                        r.TAlarm.Add(alarm);
                        r.TbRzAlarm.Add(tbRzAlarm);
                        r.SaveChanges();
                    }
                    catch { }
                }
            });
        }
        public static string GetUserName_ByPhone(string phone)
        {
            string username = string.Empty;
            try
            {
                using (renzhaContext renzha = new renzhaContext())
                {
                    TUserinfo tbde = renzha.TUserinfo.Where(c => c.Phone == phone).AsNoTracking().FirstOrDefault();
                    if (tbde != null) username = tbde.Username;
                }
            }
            catch { }
            return username;
        }
        public static int AddEnterRecord(TEnterRecord enterRecord)
        {
            int res = 0;
            try
            {
                using (renzhaContext renzha = new renzhaContext())
                {
                    renzha.TEnterRecord.Add(enterRecord);
                    res = renzha.SaveChanges();
                }
            }
            catch { }
            return res;
        }
    }
}
